package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.api.mapper.GwaMapper;
import cn.e4j.modular.common.mapper.QueryMapper;
import cn.e4j.modular.crm.entity.GwamT;
import cn.e4j.modular.crm.entity.GwcuT;
import cn.e4j.modular.crm.entity.MmraT;
import cn.e4j.modular.crm.mapper.GwamTMapper;
import cn.e4j.modular.crm.service.MmraTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 客户设备档案信息维护
 * Created by miaoyl on 2019-1-16.
 */
@Controller
@RequestMapping("/crm/gwai610")
public class Gwai610Controller extends BaseController {

    @Autowired
    private MmraTService mmraTService;

    @Autowired
    private GwamTMapper gwamMapper;

    /**
     * 跳转客户设备档案信息设置页面
     */
    @RequiresPermissions("crm:gwai610:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai610.html";
    }

    /**
     * 查询客户设备档案信息列表
     */
    @RequestMapping("/getMmralist")
    @RequiresPermissions("crm:gwai610:init")
    @ResponseBody
    public ResponseData getMmralist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ent",shiroUser.getDBC());
        inDto.put("lang",LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = mmraTService.getMmralist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询产品开窗信息列表
     */
    @RequestMapping("/getGwamList")
    @ResponseBody
    public ResponseData getGwamList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmraTService.getGwamList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询产品开窗信息列表
     */
    @RequestMapping("/getGwamAllList")
    @ResponseBody
    public ResponseData getGwamAllList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmraTService.getGwamAllList(page,inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询公司信息列表
     */
    @RequestMapping("/getSysOrgList")
    @ResponseBody
    public ResponseData getSysOrgList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmraTService.getSysOrgList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 项目开窗
     */
    @RequestMapping("/listGwemPage")
    @ResponseBody
    public ResponseData listGwemPage(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmraTService.listGwemPage(page,inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取项目开窗数据
     */
    @ResponseBody
    @RequestMapping("/getGwemData")
    public ResponseData getIQSADData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> list = mmraTService.getGwemData(inDto);
        return ResponseData.success(list);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai610:init")
    @ResponseBody
    public ResponseData add(MmraT mmraT) {
        // 判断key值是否重复
        List<MmraT> theMmraT = mmraTService.getByKey(mmraT);
        if (theMmraT.size() > 0) {
            //throw new ServiceException(CrmExceptionEnum.EXISTED_THE_CODE);
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    "序列号已存在，如需新增请删除含有该序列号的旧设备档案。");
        }
        mmraTService.add(mmraT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai610:init")
    @ResponseBody
    public ResponseData edit(MmraT mmraT,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        MmraT oldMmraT = new MmraT();
        //oldMmraT.setMMRA001(inDto.getString("oldMMRA001"));
        oldMmraT.setMMRA002(inDto.getString("oldMMRA002"));
        oldMmraT.setMMRA003(inDto.getString("oldMMRA003"));
        if(!(oldMmraT.getMMRA002().equals(mmraT.getMMRA002())
                && oldMmraT.getMMRA003().equals(mmraT.getMMRA003()) )) {
            List<MmraT> theMmraT = mmraTService.getByKey(mmraT);
            if (theMmraT.size() > 0) {
                return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                        CrmExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        mmraTService.edit(mmraT,oldMmraT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai610:init")
    @ResponseBody
    public ResponseData del(MmraT mmraT) {
        mmraTService.delete(mmraT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwai610:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmraT> mmraTList = inDto.getReqList(MmraT.class);
        mmraTService.delDataBatch(mmraTList);
        return SUCCESS_TIP;
    }

    /**
     * 计算三包到期日期
     */
    @ResponseBody
    @RequestMapping("calcuThreeDate")
    public ResponseData calcuTreeDate(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        Calendar c = Calendar.getInstance();
        try {
            QueryWrapper gwamQueryWrapper = new QueryWrapper();
            gwamQueryWrapper.eq("GWAMENT", shiroUser.getDBC());
            gwamQueryWrapper.eq("GWAM001", inDto.getString("MMRA002"));
            GwamT gwam = gwamMapper.selectOne(gwamQueryWrapper);
            Integer gwam012 = gwam.getGWAM012() == null ? 0 : Integer.valueOf(gwam.getGWAM012());//保固月数
            Integer gwam013 = gwam.getGWAM013() == null ? 0 : Integer.valueOf(gwam.getGWAM013());//保固天数
            Date mmra010 = inDto.getDate("MMRA010");//销货日期
            c.setTime(mmra010);
            c.add(Calendar.MONTH, gwam012);
            outDto.put("threeDate", DateUtils.formatDate(DateUtils.addDays(c.getTime(),gwam013)));
        }catch (Exception e){
            e.printStackTrace();
            outDto.put("threeDate","");
        }
        return ResponseData.success(outDto);
    }

}
